<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue + Axios + Servlet + Mybatis + Maven</title>
    <!--1. 添加vue依赖-->
    <script src="/webjars/vue/2.6.14/dist/vue.min.js"></script>
    <!--2. 添加axios依赖-->
    <script src="/webjars/axios/0.20.0/dist/axios.min.js"></script>
    <script src="/webjars/jquery/3.6.0/dist/jquery.min.js"></script>
    <link rel="stylesheet" href="/webjars/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="/webjars/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <style>
        .table{
            text-align: center;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <div class="panel-title">
                <h3>学生列表（Servlet + MyBatis + Maven + Vue + Axios + BS）</h3>
            </div>
        </div>
        <table class="table table-striped table-hover">
            <tr>
                <td>学号</td>
                <td>姓名</td>
                <td>性别</td>
                <td>所在班级</td>
                <td>操作</td>
            </tr>
            <tr v-for="st in students">
                <td>{{st.sid}}</td>
                <td>{{st.sname}}</td>
                <td>{{st.sex}}</td>
                <td>{{st.cname}}</td>
                <td>
                    <a href="#" class="btn btn-success btn-sm" data-toggle="modal" data-target="#myModal" @click="stud=st;title='修改学生'">修改</a>
                    <a href="" class="btn btn-danger btn-sm" @click="delStud(st.sid)">删除</a>
                </td>
            </tr>
        </table>
        <div class="panel-footer text-right">
            <a href="#" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal" @click="stud={};title='添加学生'">添加学生</a>
        </div>


    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

        <div class="modal-dialog" role="document">

            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" >{{title}}</h4>
                    --{{stud}}
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" method="post">
                        <input type="hidden" v-model="stud.sid">
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">学生姓名:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" v-model="stud.sname" placeholder="学生姓名">
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">性别:</label>
                            <div class="col-sm-10">
                                <div class="radio">
                                    <label>
                                        <input type="radio" v-model="stud.sex" value="男" checked>男
                                    </label>
                                    <label>
                                        <input type="radio" v-model="stud.sex" value="女">女
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label  class="col-sm-2 control-label">所在班级:</label>
                            <div class="col-sm-10">
                                <select class="form-control" v-model="stud.cid">
                                        <option :value="c.cid" v-for="c in classes">{{c.cname}}</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭(C)</button>
                    <button type="button" class="btn btn-primary" @click="save()">保存(S)</button>
                </div>
            </div>
        </div>
    </div>
    </div>
</div>
</body>
</html>
<script>
    let vs = new Vue({
        el: '.container',
        data:{
            students:[],            //代表所有的学生集合
            classes:[],             //所有班级列表
            stud:{},                //要添加或修改的学生对象
            title:'添加学生',
        },
        created(){                  //代表当前页面的dom元素加载完毕后自动执行的钩子函数
            //1. 查询所有学生
            this.findAllStudents();
            //2. 查询所有班级
            this.findAllClasses();
        },
        methods:{           //在这里定义我们用到的所有方法
            //1. 查询所有学生
            findAllStudents(){
                axios.get("/student?cmd=list").then(data=>{
                    console.log("student-data:",data.data);
                    this.students = data.data;
                })
            },
            //2. 查询所有班级
            findAllClasses(){
                axios.get("/classes").then(data=>{
                    console.log("class-data:",data.data);
                    this.classes = data.data;
                })
            },
            //3. 保存学生（添加或修改）
            save(){
                //3.1 根据stud.sid是否有值，决定添加还是修改
                let url = "/student?cmd=add";
                if(this.stud.sid){      //代表修改操作
                    url = "/student?cmd=update";
                }
                //3.2 向后台发送请求
                axios.post(url,"stud="+JSON.stringify(this.stud)).then(data=>{
                    console.log("add-data:",data)
                    if(data.data.code == 0){     //代表添加或修改成功，此时就刷新页面
                        //3.2.1 刷新页面
                        this.findAllStudents();
                    }
                    //3.2.2 关闭对话框
                    $('#myModal').modal('hide')
                })
            },
            //4. 删除学生
            delStud(sid){
               if(confirm('你真的要删除吗?')){ //代表点击了”确定“按钮
                   axios.get("/student?cmd=delete&sid="+sid).then(data=>{
                       if(data.data.code == 0){ //如果删除成功，就刷新页面
                           this.findAllStudents();
                       }
                   })
               }

            }
        }
    })
</script>